package com.example.appinting.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appinting.AppointActivity;
import com.example.appinting.QueueActivity;
import com.example.appinting.R;

import java.util.HashMap;
import java.util.List;

public class DoctorAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String, Object>> dataList;
    private ViewHolder vh;
    private String patient_id;




//    MyFilter mFilter ;

    private int[] pics={R.drawable.logo};
    public DoctorAdapter(Context context, List<HashMap<String, Object>> list) {
        this.context = context;
        this.dataList = list;
    }
    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        System.out.println("进入DoctorAdapter");
        vh = new ViewHolder();
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.doctor_item, null);
            vh.icon = (ImageView) view.findViewById(R.id.icon);
            vh.name = (TextView) view.findViewById(R.id.txtName);
            vh.department = (TextView) view.findViewById(R.id.department);
            vh.btn_appoint = (Button) view.findViewById(R.id.btn_appoint);

            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }

        Intent intent = ((Activity) context).getIntent();
        patient_id = intent.getStringExtra("patient_id");

        if (dataList.size() > 0) {
//            Toast.makeText(context,  String.valueOf(dataList.size()), Toast.LENGTH_SHORT).show();
            HashMap<String, Object> map = dataList.get(position);

            vh.name.setText(map.get("Doctor_name").toString());
            vh.department.setText(map.get("Department").toString());
            vh.icon.setImageResource((Integer) pics[0]);

            vh.btn_appoint.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "进入预约", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, AppointActivity.class);
                    intent.putExtra("patient_id",patient_id);
                    intent.putExtra("doctor_id",map.get("Doctor_id").toString());
                    intent.putExtra("doctor_name",map.get("Doctor_name").toString());
                    intent.putExtra("doctor_department",map.get("Department").toString());
                    context.startActivity(intent);
                }
            });
        }

        return view;
    }

    class ViewHolder {ImageView icon; TextView name, department;Button btn_appoint;
    }

    @Override
    public int getCount() {
        if (dataList != null) {
            return dataList.size(); }
        else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;

    }



}
